/*
 * File: DocumentSerializingTest.java 
 *  
 * Created on 01.02.2011
 */
package de.graph.impex;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import de.graph.document.Document;
import de.graph.document.impl.DocumentFactory;


public class DocumentSerializingTest {

    private Document document;
    
    @Before
    public void setUp() {
        this.document = DocumentFactory.create();
        
        assertNotNull(document);
        
        document.setProperty("property1", "value1");
        document.setProperty("property2", "value2");
    }

    @After
    public void tearDown() {
    }
    
    @Test
    public void testSerializing() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
        ObjectOutputStream ooStream = new ObjectOutputStream(baoStream);
        
        ooStream.writeObject(document);
        ooStream.close();
        
        System.out.println("stream size: "+ baoStream.size());
        
        ByteArrayInputStream baiStream = new ByteArrayInputStream(baoStream.toByteArray());
        ObjectInputStream oiStream = new ObjectInputStream(baiStream);
        
        Document document2 = (Document) oiStream.readObject();
        oiStream.close();
        
        assertEquals(document, document2);  // just compare IDs
        for (String propertyKey : document.getPropertyKeys()) {
            Object propertyValue  = document.getProperty(propertyKey);
            Object propertyValue2 = document2.getProperty(propertyKey);
            
            assertEquals(propertyValue, propertyValue2); 
        }
        
        System.out.println("document 1: "+ document);        
        System.out.println("document 2: "+ document2);        
    }
}
